/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#include "Faction.hpp"

#include <cassert>

#include "World.hpp"

namespace spelprog2 {
namespace sim {

Faction::Faction(World & world) :
    nextSquadId_(1),
    world_(world),
    players_(),
    squads_(),
    team_(-1)
{
    id_ = world.addFaction(*this);
}

void Faction::simUpdate(Int sequenceNbr)
{
    for (Squads::iterator it = squads_.begin(), end = squads_.end(); it != end; ++it) {
        it->second->simUpdate(sequenceNbr);
    }
}

Squad::Pointer Faction::createSquad(Int numUnits, const std::string & squadType, const Int2d & pos)
{
    //create the squad itself
    Int squadId = nextSquadId_;
    nextSquadId_ += 1;
    assert(squads_.find(squadId) == squads_.end());
    squads_[squadId] = Squad::create(id_, squadId, world_, pos, numUnits, squadType);

    world_.getSignals().squadAdded(squads_[squadId], id_);
	return squads_[squadId];
}

void Faction::updateTeam(Int team)
{
    team_ = team;

    if (team >= Int(0)) {
        for (Squads::iterator it = squads_.begin(), end = squads_.end(); it != end; ++it) {
            it->second->setTeam(team);
        }
    }
}

}; //sim
}; //spelprog2
